package ysq.xyg.ticket.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.ticket.dao.TicketOrderDao;
import ysq.xyg.ticket.model.po.TicketOrder;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.ticket.model.vo.TicketOrderVo;
import ysq.xyg.ticket.service.TicketOrderService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 票卷模块-订单信息service实现
 *
 * @author shengQiang yu
 * @date 2019-10-30 10:23:26
 */
@Service("ticketOrderService")
public class TicketOrderServiceImpl extends BaseServiceImpl<TicketOrder> implements TicketOrderService {

    @Resource
    private TicketOrderDao ticketOrderDao;

    private TicketOrder ticketOrder;

    @Autowired
    public void setBaseDao(TicketOrderDao ticketOrderDao) {
        this.baseDao = ticketOrderDao;
    }


    /**
     * 分页查询
     *
     * @param ticketOrderVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-30 10:23:26
     */
    public ReturnEntity page(TicketOrderVo ticketOrderVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<TicketOrder> list = acceptList(ticketOrderVo);
        PageInfo<TicketOrder> page=new PageInfo<TicketOrder>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param ticketOrderVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-30 10:23:26
     */
    public ReturnEntity addOrUpdate(TicketOrderVo ticketOrderVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(ticketOrderVo.getId())){
            ticketOrder=new TicketOrder(currentUser);
            isUpate=false;
        }else{
            ticketOrder=selectByPrimaryKey(ticketOrderVo.getId());
            if(ObjectUtil.isNull(ticketOrder)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            ticketOrder.setUpdateTime(new Date());
            ticketOrder.setUpdateUser(currentUser.getId());
        }
        ticketOrder.setName(ticketOrderVo.getName());
        ticketOrder.setThumImage(ticketOrderVo.getThumImage());
        ticketOrder.setTicketProductId(ticketOrderVo.getTicketProductId());
        ticketOrder.setMemberId(ticketOrderVo.getMemberId());
        ticketOrder.setStatus(ticketOrderVo.getStatus());
        ticketOrder.setBuyNum(ticketOrderVo.getBuyNum());
        ticketOrder.setUnitPrice(ticketOrderVo.getUnitPrice());
        ticketOrder.setCouponMoney(ticketOrderVo.getCouponMoney());
        ticketOrder.setCouponId(ticketOrderVo.getCouponId());
        ticketOrder.setResultMoney(ticketOrderVo.getResultMoney());
        ticketOrder.setPaymentType(ticketOrderVo.getPaymentType());
        ticketOrder.setBuyerMessage(ticketOrderVo.getBuyerMessage());
        ticketOrder.setPaymentTime(ticketOrderVo.getPaymentTime());
        ticketOrder.setWechatOrderNo(ticketOrderVo.getWechatOrderNo());
        ticketOrder.setLinkName(ticketOrderVo.getLinkName());
        ticketOrder.setLinkPhone(ticketOrderVo.getLinkPhone());
        ticketOrder.setUseMemberId(ticketOrderVo.getUseMemberId());
        ticketOrder.setUseTime(ticketOrderVo.getUseTime());
        ticketOrder.setReturnMessage(ticketOrderVo.getReturnMessage());
        ticketOrder.setServiceState(ticketOrderVo.getServiceState());
        ticketOrder.setServiceMessage(ticketOrderVo.getServiceMessage());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(ticketOrder);
        }else{
            num=insert(ticketOrder);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-30 10:23:26
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        TicketOrder ticketOrder=selectByPrimaryKey(id);
        if(ticketOrder!=null){
            ticketOrder.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            ticketOrder.setUpdateTime(new Date());
            ticketOrder.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(ticketOrder);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param ticketOrderVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-10-30 10:23:26
     */
    public List<TicketOrder> acceptList(TicketOrderVo ticketOrderVo){
        Example example = new Example(TicketOrder.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(ticketOrderVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",ticketOrderVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return ticketOrderDao.selectByExample(example);
    }
}
